//
//  NSString+EliminateZero.m
//  Stars
//
//  Created by 魏鹏 on 16/12/19.
//  Copyright © 2016年 hst. All rights reserved.
//

#import "NSString+EliminateZero.h"

@implementation NSString (EliminateZero)

- (NSString *)eliminateZeroWithDouble:(NSInteger)integer{
    
    NSString *str = [self copy];
    
    double fdouble = [str doubleValue];
    
    NSString *ftotal;
    switch (integer) {
        case 1:
            ftotal = [NSString stringWithFormat:@"%.1f", fdouble];
            break;
        case 2:
            ftotal = [NSString stringWithFormat:@"%.2f", fdouble];
            break;
        case 3:
            ftotal = [NSString stringWithFormat:@"%.3f", fdouble];
            break;
        case 4:
            ftotal = [NSString stringWithFormat:@"%.4f", fdouble];
            break;
        case 5:
            ftotal = [NSString stringWithFormat:@"%.5f", fdouble];
            break;
        default:
            break;
    }

    while ([ftotal hasSuffix:@"0"]) {
        ftotal = [ftotal substringToIndex:[ftotal length]-1];
    }
    
    if ([ftotal hasSuffix:@"."]) {
        ftotal = [ftotal substringToIndex:[ftotal length]-1];
    }
    
    return ftotal;
    
}

- (NSString *)getDealNumwithstring:(NSString *)string withNumCount:(NSInteger)integer{
    
    
    NSDecimalNumber *numberA = [NSDecimalNumber decimalNumberWithString:string];
    NSDecimalNumber *numberB ;
    if (integer == 4) {
        numberB =  [NSDecimalNumber decimalNumberWithString:@"10000"];
    } else if (integer == 8){
        numberB =  [NSDecimalNumber decimalNumberWithString:@"100000000"];
    }else{
        numberB =  [NSDecimalNumber decimalNumberWithString:@"10000"];
    }
    //NSDecimalNumberBehaviors对象的创建  参数 1.RoundingMode 一个取舍枚举值 2.scale 处理范围 3.raiseOnExactness  精确出现异常是否抛出原因 4.raiseOnOverflow  上溢出是否抛出原因  4.raiseOnUnderflow  下溢出是否抛出原因  5.raiseOnDivideByZero  除以0是否抛出原因。
    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    /// 这里不仅包含Multiply还有加 减 乘。
    NSDecimalNumber *numResult = [numberA decimalNumberByDividingBy:numberB withBehavior:roundingBehavior];
    NSString *strResult = [numResult stringValue];
    return strResult;

//    NSString * dealStr;
//    double fdouble = 0;
//    if (integer == 4) {
//        fdouble =  [string doubleValue]/10000;
//    } else if (integer == 8){
//        fdouble =  [string doubleValue]/100000000;
//    }else{
//        fdouble =  [string doubleValue]/10000;
//    }
//    NSNumber *num = [NSDecimalNumber numberWithDouble:fdouble];
//    NSString * str = [num stringValue];
//    dealStr = [str eliminateZeroWithDoubleTosub:2];
//    if (integer == 8) {
//        dealStr = [NSString stringWithFormat:@"%@%@",@"约",dealStr];
//    }
//    return dealStr;
}


- (NSString *)eliminateZeroWithDoubleTosub:(NSInteger)integer{

    NSString *str = [self copy];
    
    NSArray *arr = [str componentsSeparatedByString:@"."];
    if (arr.count == 2 ) {//含有小数
        NSString *fNum = arr[0];//整数位
        NSString *dNum = arr[1];//小数位
        if (dNum.length>=integer) {
            str = [str substringToIndex:(fNum.length+integer+1)];
        } else {
            str = [str substringToIndex:(fNum.length+dNum.length+1)];
        }
        
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
        
        if ([str hasSuffix:@"."]) {
            str = [str substringToIndex:[str length]-1];
        }

    }else if (arr.count == 1){//不含小数
        str = str;
    }

    return str;

}

- (BOOL)judgeStringIsZero{
    
    NSString *str = [self copy];
    NSArray *arr = [str componentsSeparatedByString:@"."];
    if (arr.count == 2 ) {//含有小数
        
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
        
        if ([str hasSuffix:@"."]) {
            str = [str substringToIndex:[str length]-1];
        }
        
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
        
    }else if (arr.count == 1){//不含小数
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
    }
    
    if ([str isEqualToString:@"0"] || str.length == 0) return YES;
    
    return NO;
    
}

- (CGFloat)getIntegerNstringCount{
    
    NSString *numStr = [self copy];
    NSArray *arr = [numStr componentsSeparatedByString:@"."];
    if (arr.count == 2 || arr.count == 1) {
        NSString *fNum = arr[0];//整数位
        CGFloat ai = fNum.length;
        return ai;
    } else {
        return 0;
    }
}


@end
